package com.payment.controller;


import com.payment.model.UserInfo;
import com.payment.service.OrderService;
import com.payment.service.UserService;
import com.payment.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@CrossOrigin
@Controller
public class HomeController {
    @Autowired
    UserService userService;

    @Autowired
    OrderService orderService;

    @RequestMapping("/")
    public ModelAndView Default(){
        ModelAndView mv = new ModelAndView("index");

        return mv;
    }


    @RequestMapping("index")
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView("index");
        List<UserInfo> userlist = userService.getAllUsers();
        for(UserInfo u:userlist){
            System.out.println(u.getUsername());
        }

        return mv;
    }

    /**
     * 前后的分离
     * 接口模板
     * */
    @ResponseBody
    @RequestMapping(value = "/testlogin", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response template(@RequestBody UserInfo userInfo){
        System.out.println("请求成功");
        System.out.println(userInfo.getUsername());
        return new Response(200,"测试成功",userInfo.getUsername());
    }

    /**
     * 注册方法
     *
     * */
    @ResponseBody
    @RequestMapping(value = "/regUser", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response regUser(@RequestBody UserInfo userInfo){
        return userService.RegUser(userInfo);
    }

    /**
     * 登录验证
     * @参数  用户名和密码
     *
     * */
    @ResponseBody
    @RequestMapping(value = "/validateLogin", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response validateLogin(@RequestBody UserInfo userInfo){
        return userService.validateLogin(userInfo);
    }

    /**
     * 生成个人订单编号
     * */
    @ResponseBody
    @RequestMapping(value = "/randomOrderCode", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response randomOrderCode(@RequestBody UserInfo userInfo){
        return orderService.addUserOrderCode(userInfo);
    }

    /**
     * 扫码扣款方法：payment
     *
     * */
    @ResponseBody
    @RequestMapping(value = "/payment", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response payment(@RequestBody UserInfo userInfo){
        return orderService.payment(userInfo);
    }
}
